package com.lanou.beautifulwardrobe.home;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.Response;
import com.baoyz.widget.PullRefreshLayout;
import com.google.gson.Gson;
import com.lanou.beautifulwardrobe.R;
import com.lanou.beautifulwardrobe.home.adapter.ChoiceRecyclerViewAdapter;
import com.lanou.beautifulwardrobe.view.Tools;
import com.lanou.beautifulwardrobe.activity.ProductDetailActivity;
import com.lanou.beautifulwardrobe.contants.Contants;
import com.lanou.beautifulwardrobe.contants.VolleySingleton;
import com.lanou.beautifulwardrobe.entify.ChoiceProduct;
import com.lanou.beautifulwardrobe.entify.ChoiceResult;
import com.lanou.beautifulwardrobe.listener_interface.MyHotlistItemClickListener;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * Created by dllo on 15/10/22.
 */
public class OtherFragment extends Fragment {

    private RecyclerView otherRecyclerView;
    private StaggeredGridLayoutManager staggeredGM;
    private ChoiceRecyclerViewAdapter choiceRecyclerViewAdapter;
    private VolleySingleton volleySingleton = VolleySingleton.getInstance();
    private String itemTitle;
    private String id;
    private PullRefreshLayout otherRefreshableView;

    public OtherFragment(String itemIitle, String id) {
        this.itemTitle = itemIitle;
        this.id = id;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.maintab_home_item_other, null);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        otherRecyclerView = (RecyclerView) view.findViewById(R.id.otherRecyclerView);
        otherRefreshableView = (PullRefreshLayout) view.findViewById(R.id.otherRefreshableView);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        otherRefreshableView.setRefreshStyle(PullRefreshLayout.STYLE_CIRCLES);
        otherRefreshableView.setOnRefreshListener(new PullRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                otherRefreshableView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Tools.getProgress(getActivity());
                        initView();
                        initData();
                        otherRefreshableView.setRefreshing(false);
                    }
                }, 0);
            }
        });

        initView();
        initData();
    }

    private void initView() {
        staggeredGM = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        choiceRecyclerViewAdapter = new ChoiceRecyclerViewAdapter();
        choiceRecyclerViewAdapter.setOnMyItemClickListener(new MyHotlistItemClickListener() {
            @Override
            public void onHotlistItemClick(String id) {
                Intent intent = new Intent(getActivity(), ProductDetailActivity.class);
                intent.putExtra("id", id);
                getActivity().startActivity(intent);
            }
        });
    }

    private void initData() {
        otherRecyclerView.setLayoutManager(staggeredGM);
        otherRecyclerView.setAdapter(choiceRecyclerViewAdapter);

        String str = "";
        try {

            str = java.net.URLEncoder.encode(itemTitle, "utf-8");
            // str = java.net.URLDecoder.decode(itemTitle, "UTF-16");
            // Log.i("lanou", str + "啊哈哈");
            String url = Contants.CHOICE_URL_PREFIX + id + "&keyword=" + str + Contants.CHOICE_URL_SUFFIX;


            Log.i("lanou", url + "@@@@@");
            volleySingleton.getStringRequset(url, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Gson gson = new Gson();
                    // Log.i("lanouuu", response.toString());
                    ChoiceResult choiceResult = gson.fromJson(response, ChoiceResult.class);
                    if (choiceResult != null) {
                        // Log.i("lanou", choiceResult + "&&&&");
                        // Log.i("lanou", choiceResult.getData().getProducts() + "%%%%");
                        List<ChoiceProduct> choiceProducts = choiceResult.getData().getProducts();
                        // Log.i("lanou", choiceProducts + "****");
                        choiceRecyclerViewAdapter.addData(choiceProducts);
                        Tools.progressDialog.dismiss();
                    }
                }
            });
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }


}
